/**
 *
 * @param {*} val 当前要格式化的时间
 * @param {*} type 是完整的时间还是，只有年月日； type = m 是完整的时间
 * @returns
 */
const formatDate = (val, type = 'm') => {
  var time = val == null ? "" : Date.parse(val.replace(/-/g, '/'));
  var date = new Date(time);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return type == 'm' ? `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}` : `${[year, month, day].map(formatNumber).join('-')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//非空判断
const checkEmpty = value => {
  if (value === '' || value === null || typeof (value) == 'undefined') {
    return true
  }
  return false
}

//手机号中间4位隐藏
const handlePhoneNumber = value => {
  const start = value.slice(0, 3);
  const end = value.slice(-4);
  return `${start}****${end}`;
}
//身份证号中间8位隐藏
const handleIdCard = value => {
  return value.replace(/(.{6}).*(.{4})/, '$1********$2')
}

//手机号格式判断
const checkPhone = value => {
  let reg = /^1[0-9]{10}$/
  if (reg.test(value)) {

    return true
  }
  return false
}

//判断字符串中是否全是数字
const is_numeric = (value) => {
  if (typeof (value) === 'object') {
    return false;
  } else {
    return !Number.isNaN(Number(value));
  }
};

//扫码scene转码,和后端约定，扫码的参数scene为doctorId-12~patientId-12~number-ll的形式
const formartScene = scene => {
  let paramList = scene.split('~')
  let options = {}
  paramList.forEach((val) => {
    let param = val.split('-')
    options[param[0]] = param[1]
  })
  return options

}
//时间戳转换为月日
const timestampToData = timestamp => {
  const time = timestamp * 1000
  const date = new Date(time)
  const month = date.getMonth() + 1
  const day = date.getDate()
  return month + '-' + day
}


// 生成唯一ID
const getGuid_nanoid = () => {
  let now = new Date().getTime();
  let str = `xxxxxxxxxxxx${now}yxxx`;
  // let str = `xxxxxxxx-xxxx-${now}-yxxx`;
  return str.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16)
  })
}

module.exports = {
  checkEmpty,
  handlePhoneNumber,
  handleIdCard,
  checkPhone,
  is_numeric,
  formartScene,
  timestampToData,
  formatDate,
  getGuid_nanoid,

}